﻿using System;
using System.Collections;
using System.Configuration;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PlugNT.Common;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;

namespace PlugNT.Cms.Web
{
    public partial class _Comment : System.Web.UI.Page
    {
        public string tableid = "";
        public string articeid = "";
        private int aid = 0;
        private int pagesize = 10;

        public string commenttitle = "";
        public string commenlist = "";
        public string pagination = "";
        public string currurl = "Comment.aspx";
        public string commentcount = "";

        private int page = 0;
        private string pageargs = "";

        private Comment curComment = new Comment();
        private Article curArticle = new Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            string error = InitData();
            if (error != "")
            {
                PlugNT.Common.JScript.AlertAndRedirect(error.Replace("'",""), "/");
            }
        }

        private string InitData()
        {
            string error = "";
            try
            {
                tableid = (Request.QueryString["tid"] == null) ? "0" : Request.QueryString["tid"].Trim();
                articeid = (Request.QueryString["aid"] == null) ? "0" : Request.QueryString["aid"].Trim();
                aid = int.Parse(articeid);
                int tid = (tableid == "") ? 0 : int.Parse(tableid);
                if (tid >= 0)
                {
                    page = (Request.QueryString["page"] == null) ? 0 : int.Parse(Request.QueryString["page"].Trim());
                    pageargs = "tid=" + tableid + "&aid=" + articeid + "&page";

                    commenttitle = curArticle.GetTitle(aid);
                    if (commenttitle != "")
                    {
                        int recordCount = 0;

                        curComment.CurPostfix = (tid == 0) ? "" : tid.ToString();   //表后缀
                        DataTable dt = curComment.GetList(pagesize, (page - 1), aid, 0, out recordCount);
                        if (dt.Rows.Count > 0)
                        {
                            commentcount = dt.Rows.Count.ToString();
                            pagination = PlugNT.Common.Pager.Pagination(recordCount, pagesize, page, currurl, pageargs,"",true, true);
                            StringBuilder sb = new StringBuilder();
                            foreach (DataRow dr in dt.Rows)
                            {
                                string str = "<tr align=\"center\">\n" +
                                  " <td class=\"comment\">\n" +
                                  "  <table width=\"100%\">\n" +
                                  "      <tr>\n" +
                                  "          <td align=\"left\">" + dr["username"].ToString() + "</td><td align=\"right\">" + dr["addtime"].ToString() + "发表</td>\n" +
                                  "      </tr>\n" +
                                  "      <tr>\n" +
                                  "          <td colspan=\"2\" align=\"left\">" + dr["content"].ToString() + "</td>\n" +
                                  "      </tr>\n" +
                                  "      <tr>\n" +
                                  "          <td colspan=\"2\" align=\"right\" class=\"res\"><span id='tp" + dr["id"].ToString() + "' onclick=\"Tip('" + dr["id"].ToString() + "');\" >顶[" + dr["tip"].ToString() + "]</span>&nbsp;<span id='bak" + dr["id"].ToString() + "' onclick=\"Reply('" + dr["id"].ToString() + "','" + dr["username"].ToString() + "');\">盖楼</span></td>         \n" +
                                  "      </tr>\n" +
                                  "  </table>\n" +
                                  " </td>\n" +
                                  "</tr>\n";
                                sb.Append(str);
                            }
                            commenlist = sb.ToString();
                        }
                    }
                    else
                    {
                        error = "您评论的文章不存在或已经删除！";
                    }
                }
                else
                {
                    error = "您查看的评论不存在或已经删除！";
                }



            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

    }
}
